#include <stdio.h>
#include <assert.h>
#include <unistd.h>

#include "tlm_srv.h"
#include "broker.h"
#include "common_data.h"

int tlm_start(){
    int sock;
    struct sockaddr_in addr;
    int sock_buf_size = 128;

    assert((sock = socket(AF_INET, SOCK_DGRAM, 0)) != -1);

    addr.sin_family = AF_INET;
    addr.sin_port = htons(TLM_SERVER_PORT);
    //addr.sin_addr.s_addr = htonl(TLM_SERVER_IP);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    //addr.sin_addr.s_addr = inet_addr("192.168.20.10");

    assert(fcntl(sock, F_SETFL, O_NONBLOCK, 1) != -1);
    assert(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &sock_buf_size, sizeof(int)) != -1);
    assert(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != -1);

    return sock;
}

int main(){
  int sock = tlm_start();

  for(int i = 0; i < 100; ++i){
    perror("");
    gcs_tlm_t tlm;

    recv_tlm(sock, &tlm);
    
    if(i > 0)
      gcs_tlm_print(tlm);

    usleep(1e6);
  }


  return 0;
}
